<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of TeachingController
 *
 * @author gc
 */
class TeachingController extends AdminController
{

    protected function _initialize()
    {
        if ('sharecomplete' != ACTION_NAME) {
            parent::_initialize();
        }
    }

    public function index()
    {
        $model = M("Class_pay");

        //获取已缴费的班级id
        $array = $model->field('id')->where('type=2 and status=1')->group('grade_id')->select();
        $ids = '';
        foreach ($array as $v) {
            $ids .= $v['id'] . ',';
        }
        $ids = trim($ids, ',');


        $map['status'] = 1;
        $map['type'] = 1;

        $list = $this->lists($model, $map, 'status asc,create_time desc');
        $this->assign('_list', $list);

        $this->meta_title = '一对一教学计划';

        $this->display();
    }

    public function add()
    {
        $id = I("id");

        $pay = M("Class_pay")->where("id=" . $id)->find();

        $student = D("Student")->getStudentById($pay['sid']);


        $list = M("class_plan")->where("pay_id=" . $pay['id'])->order("id asc")->select();

        $this->assign('_list', $list);


        $this->meta_title = $student['name'] . "《" . _getCourseTitle($pay['course_id']) . '》教学计划';
        $this->showTeacher();

        $this->display();
    }

    private function showTeacher()
    {
        $list = M("Member")->select();
        $this->assign("_teacher", $list);
    }

    public function ajaxSaveData()
    {
        $id = I("id");
        $class_time = I("class_time");
        $teacher = I("teacher");
        $data = [
            'class_time' => strtotime($class_time),
            'teacher_id' => $teacher,
            'update_time' => time()
        ];
        $s = M("class_plan")->where("id=" . $id)->save($data);
        $this->ajaxReturn(['status' => $s ? 1 : 0]);
    }

    //备课
    public function lesson()
    {
        $id = I("id");
        $plan = D("ClassPlan")->getClassPlanById($id);
        if (!empty($plan['pay_id'])) {

            $pay = M("Class_pay")->where("id=" . $plan['pay_id'])->find();
            $meta_title = _getStudentName($pay['sid']) . '《' . _getCourseTitle($pay['course_id']) . '》教学方案';
        } else {
            $meta_title = _getGradeName($plan['grade_id']) . '教学方案';
        }

        $this->assign("plan", $plan);
        $this->assign("id", $id);
        $this->meta_title = $meta_title;
        $this->display();
    }

    //保存教学方案
    public function saveLesson()
    {
        $id = I("id");
        $pay_id = I("pay_id");
        $teach_plan = I("teach_plan");
        $data = [
            'step' => 2,
            'teach_plan' => $teach_plan,
            'lesson_time' => time(),
            'update_time' => time()
        ];

        if (empty($teach_plan)) {
            $teach_situation = I("teach_situation");
            $homework = I("homework");
            $roll_call_all = I("roll_call_all");
            $roll_call = I("roll_call");
            $data['step'] = 3;
            $data['teach_situation'] = $teach_situation;
            $data['homework'] = $homework;
            $data['roll_call'] = implode(',', $roll_call);
            $data['roll_call_all'] = trim($roll_call_all, ',');
            unset($data['teach_plan']);

            //回访
            $return_visit = I("return_visit");
            if (empty($teach_situation) || !empty($return_visit)) {
                $data = ['step' => 4, 'return_visit' => $return_visit];
            }
        }

        $s = M("Class_plan")->where("id=$id")->save($data);

        if (!empty($pay_id)) {

            $where['step'] = ['lt', 3];
            $where['pay_id'] = $pay_id;
            $num = M("Class_plan")->where($where)->count('id');
            if ($num == 0) {
                //完成教学计划
                M("Class_pay")->where("id=" . $pay_id)->save(['status' => 2, 'update_time' => time()]);
            }
            $url = U("add?id=$pay_id");
        } else {
            $plan = M("Class_plan")->find($id);
            $url = U("tomanyadd?grade_id=" . $plan['grade_id']);
        }

        $s ? $this->success("操作成功！", $url) : $this->error("操作失败！");
    }

    //完成
    public function complete()
    {
        $id = I("id");
        $ck = I("ck");
        $plan = D("ClassPlan")->getClassPlanById($id);

        if (!empty($plan['pay_id'])) {

            $pay = M("Class_pay")->where("id=" . $plan['pay_id'])->find();
            $meta_title = _getStudentName($pay['sid']) . '《' . _getCourseTitle($pay['course_id']) . '》教学完成';
        } else {
            $meta_title = _getGradeName($plan['grade_id']) . '教学完成';

            //获取学生
            $map['grade_id'] = $plan['grade_id'];
            $map['end_time'] = ['gt', strtotime(date("Y-m-d", time()))];
            $student = M("Grade_student")->where($map)->select();
            $this->assign("student", $student);
        }


        $this->assign("ck", $ck);
        $this->assign("hf", I("hf")); //回访
        $this->assign("plan", $plan);
        $this->assign("id", $id);
        $this->meta_title = $meta_title;
        $this->display();
    }

    //教学完成分享
    public function sharecomplete()
    {
        $id = I("id");
        $ck = I("ck");
        $plan = D("ClassPlan")->getClassPlanById($id);

        if (!empty($plan['pay_id'])) {

            $pay = M("Class_pay")->where("id=" . $plan['pay_id'])->find();
            $meta_title = _getStudentName($pay['sid']) . '《' . _getCourseTitle($pay['course_id']) . '》教学完成情况';
        } else {
            $meta_title = _getGradeName($plan['grade_id']) . '教学完成情况';
        }


        $this->assign("ck", $ck);
        $this->assign("plan", $plan);
        $this->assign("id", $id);
        $this->meta_title = $meta_title;
        $temp = isMobile() ? 'sharecomplete2' : 'sharecomplete';
        $this->display($temp);
    }

    //备课参考
    public function reference()
    {
        $course_plan_id = I("course_plan_id");
        $map['course_plan_id'] = array('eq', $course_plan_id);
        $map['_string'] = "teach_plan <> '' or teach_plan is not null";

        $list = $this->lists('Class_plan', $map, 'lesson_time desc');
        $this->assign('_list', $list);

        $this->meta_title = '教学方案参考';

        $this->display();
    }

    /*     * ******一对多********* */

    public function tomany()
    {
        $model = M("Class_pay");

        //获取已缴费的班级id
        $array = $model->alias('p')->field('p.id')->join('__GRADE__ g ON g.id = p.grade_id')->where('p.type=2 and p.status=1 and g.status=2')->group('grade_id')->select();
        $idArray = [];
        foreach ($array as $v) {
            array_push($idArray, $v['id']);
        }

        if (count($idArray) > 0) {
            $map['id'] = ['in', $idArray];
            $list = $this->lists($model, $map, 'status asc,create_time desc');
        }
        $this->assign('_list', $list);

        $this->meta_title = '一对多教学计划';

        $this->display();
    }

    public function tomanyadd()
    {
        $grade_id = I("grade_id");

        $list = crtToManyTeach($grade_id);
        $this->assign('_list', $list);

        //获取班级信息
        $grade = M("Grade")->find($grade_id);

        $this->meta_title = $grade['name'] . ' 教学计划';
        $this->showTeacher();
        $this->display();
    }

}
